package com.ybwh.springboot2.util;

import com.google.common.base.Preconditions;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import java.io.UnsupportedEncodingException;
import java.security.SecureRandom;

/**
 * <p>
 * DES加密解密
 * </p>
 *
 * @author fanbeibei
 * @version V1.0
 * @since 2020/3/18 13:36
 */
@Slf4j
public class DesUtil {

    private static final String PASSWORD_CRYPT_KEY = "kEHrDooxWHCWtfeSxvDvgqZq";
    private final static String ALGORITHM = "DES";
    private final static String charsetName = "UTF-8";


    /**
     * 解密
     *
     * @param data 数据
     * @param key  密钥
     * @return
     * @throws Exception
     */
    public final static String decrypt(String data, String key) throws Exception {
        return new String(decrypt(hex2byte(data), key.getBytes(charsetName)),
                charsetName);
    }

    /**
     * 加密
     *
     * @param data 数据
     * @param key  密钥
     * @return
     * @throws Exception
     */
    public final static String encrypt(String data, String key) throws Exception {
        return byte2hex(encrypt(data.getBytes(charsetName), key.getBytes(charsetName)));
    }


    /**
     * 加密
     *
     * @param data 数据
     * @param key  密钥
     * @return
     */
    private static byte[] encrypt(byte[] data, byte[] key) {
        try {
            SecureRandom sr = new SecureRandom();
            DESKeySpec dks = new DESKeySpec(key);
            SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(ALGORITHM);
            SecretKey securekey = keyFactory.generateSecret(dks);
            Cipher cipher = Cipher.getInstance(ALGORITHM);
            cipher.init(Cipher.ENCRYPT_MODE, securekey, sr);
            return cipher.doFinal(data);
        } catch (Exception e) {
            System.err.print("encrypt fail:\n" + e);
            return null;
        }
    }


    /**
     * 解密
     *
     * @param data 数据
     * @param key  密钥
     * @return
     */
    private static byte[] decrypt(byte[] data, byte[] key) throws Exception {
        SecureRandom sr = new SecureRandom();
        DESKeySpec dks = new DESKeySpec(key);
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(ALGORITHM);
        SecretKey securekey = keyFactory.generateSecret(dks);
        Cipher cipher = Cipher.getInstance(ALGORITHM);
        cipher.init(Cipher.DECRYPT_MODE, securekey, sr);
        return cipher.doFinal(data);
    }


    /**
     * 16进制转byte
     *
     * @param hexData 16进制字符串
     * @return
     */
    public static byte[] hex2byte(String hexData) throws UnsupportedEncodingException {
        Preconditions.checkArgument(StringUtils.isNotBlank(hexData));
        byte[] b = hexData.getBytes(charsetName);
        Preconditions.checkArgument((b.length % 2) == 0, "the length is not even");


        byte[] b2 = new byte[b.length / 2];
        for (int n = 0; n < b.length; n += 2) {
            String item = new String(b, n, 2);
            b2[n / 2] = (byte) Integer.parseInt(item, 16);
        }
        return b2;
    }


    /**
     * byte 转  16进制 字符串
     *
     * @param b
     * @return
     */
    public static String byte2hex(byte[] b) {
        String hs = "";
        String stmp = "";
        for (int n = 0; n < b.length; n++) {
            stmp = (java.lang.Integer.toHexString(b[n] & 0XFF));
            if (stmp.length() == 1) {
                hs = hs + "0" + stmp;
            } else {
                hs = hs + stmp;
            }
        }
        return hs.toUpperCase();
    }


}
